package com.polyvore.io;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpParams;

import android.os.Bundle;
import android.text.Html;
import android.util.Log;

public class HttpConnection {
	public static String open(String url, String method, Bundle params) throws ClientProtocolException, IOException, URISyntaxException{
		Log.d("URL",url);
		String responseStringResult = "";
		method = method.toUpperCase();
		if(method.equals("POST")){
			DefaultHttpClient hc = new DefaultHttpClient();  
            ResponseHandler <String> res=new BasicResponseHandler();  
            HttpPost postMethod=new HttpPost(url.toString()); 
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            for (String key : params.keySet()) {
            	nameValuePairs.add(new BasicNameValuePair(key, params.getString(key)));
            }    
          
            postMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs));
			Log.d("openConnection->url", url);
			responseStringResult = hc.execute(postMethod,res); 
		} else if(method.equals("GET")){
			HttpClient client = new DefaultHttpClient();
			if(params!=null)
			{
				List<NameValuePair> paramsQuery = new ArrayList<NameValuePair>();
				for (String key : params.keySet()) {
	            	paramsQuery.add(new BasicNameValuePair(key,params.getString(key)));
	            } 
				
				String query = URLEncodedUtils.format(paramsQuery, "utf-8");
				
				if(!url.endsWith("?"))
			        url += "?";
				
				url+= query;
			}
			HttpGet request = new HttpGet();
			request.setURI(new URI(url));
			
			HttpResponse response;
			
             
			Log.d("openConnection->url", url);
			response = client.execute(request);
			responseStringResult = read(response.getEntity().getContent());
		}
		else if(method.equals("DELETE"))
		{
			HttpClient hc = new DefaultHttpClient();  
            ResponseHandler <String> res=new BasicResponseHandler();  
            HttpDelete deleteMethod=new HttpDelete(url.toString()); 
            HttpParams httpParams = new BasicHttpParams();
            for (String key : params.keySet()) {
            	httpParams.setParameter(key, params.getString(key));
            }    
            deleteMethod.setParams(httpParams);
			responseStringResult = hc.execute(deleteMethod,res); 
		}
		return Html.fromHtml(responseStringResult).toString();
	}
	
	public static String read(InputStream in) throws IOException {
        StringBuilder sb = new StringBuilder();
        BufferedReader r = new BufferedReader(new InputStreamReader(in), 1024*1024);
        for (String line = r.readLine(); line != null; line = r.readLine()) {
            sb.append(line);
        }
        in.close();
        return sb.toString();
    }
}
